package cn.zhl.设计模式.适配器.springmvc;

import cn.zhl.设计模式.适配器.springmvc.adapter.BasicAdapter;
import cn.zhl.设计模式.适配器.springmvc.adapter.HttpRequestAdapter;
import cn.zhl.设计模式.适配器.springmvc.adapter.SimleAdapter;
import cn.zhl.设计模式.适配器.springmvc.controller.Controller;
import cn.zhl.设计模式.适配器.springmvc.controller.HttpRequestController;
import cn.zhl.设计模式.适配器.springmvc.controller.SimpleController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: demos
 * @description:
 * @author: 刘振华
 * @create: 2021-03-17 16:48
 **/
public class DispatcherServlet {
	public static void main(String[] args) {
		new DispatcherServlet().doDispatch();
	}
	public static List<BasicAdapter> adapters = new ArrayList<>();

	public DispatcherServlet(){
		adapters.add(new SimleAdapter());
		adapters.add(new HttpRequestAdapter());
	}
	public void doDispatch(){
//		SimpleController controller = new SimpleController();
		HttpRequestController controller = new HttpRequestController();
		BasicAdapter handler = getHandler(controller);
		if(handler != null){
			handler.handle(controller);
		}
	}

	private BasicAdapter getHandler(Controller controller){
		for(BasicAdapter ba : adapters){
			if(ba.support(controller)){
				return ba;
			}
		}
		return null;
	}
}
